package com.vvlhw.pptmall.product.config;

import com.google.common.base.Predicate;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author VVlhw
 * @date 2020/6/19 - 20:32
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment) {
        Profiles of = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .groupName("梁汉伟部分")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.vvlhw.pptmall.product.controller"))
                .apis(new Swaggerfilter())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("梁汉伟", "https://vvlhw.com", "15362941982@163.com");
        return new ApiInfo(
                "PPT_Mall的接口文档",
                "Stay Hungry Stay Foolish",
                "v1.0",
                "https://vvlhw.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );

    }

    /**
     *     一个只看有 @ApiOperation 的过滤器
      */
    private class Swaggerfilter implements Predicate<RequestHandler> {

        @Override
        public boolean apply(@NullableDecl RequestHandler requestHandler) {
            if (requestHandler.findAnnotation(ApiOperation.class).isPresent()) {
                return true;
            }
            return false;
        }
    }
}
